import openpyxl
from flask import jsonify

from dao.nurse import patient_list_dao

def list_read():
    result = patient_list_dao.read()

    if result:
        user_first = result
        print(user_first)
        result = {"code": 20000, "err": 0, "message": "查看心理量表成功",
                  "data": {"userinfo": user_first}}
    else:
        result = {"err": 2, "message": "访问心理量表失败", "data": {}}
    return result

def id_delete(data):
    id = data["id"]
    print(id)
    result = patient_list_dao.id_delete(id)

    if result:
        user_first = result
        print(user_first)
        result = {"code": 20000, "err": 0, "message": "删除成功",
                  "data": {"userinfo": user_first}}
    else:
        result = {"err": 2, "message": "删除失败", "data": {}}
    return result

def add(data):
    result = patient_list_dao.add(data)

    if result:
        user_first = result
        print(user_first)
        result = {"code": 20000, "err": 0, "message": "新增成功",
                  "data": {"userinfo": user_first}}
    else:
        result = {"err": 2, "message": "新增失败", "data": {}}
    return result

def update(data):
    id = data.pop('id', None)

    # 去掉创建时间和修改时间的修改,因为数据库是自动新增的
    data.pop('create_time')
    data.pop('update_time')
    print(f'打印{data}')

    result = patient_list_dao.update(data, id)

    if result:
        user_first = result
        print(user_first)
        result = {"code": 20000, "err": 0, "message": "修改成功",
                  "data": {"userinfo": user_first}}
    else:
        result = {"err": 2, "message": "修改失败", "data": {}}
    return result

def importing(file):

    # 如果用户没有选择文件，浏览器也可能提交一个没有文件名的空部分
    # 所以我们需要检查文件是否为空
    if file.filename == '':
        return jsonify({"error": "No selected file"}), 400

    if file:
        try:
            # 使用 openpyxl 加载 Excel 文件
            workbook = openpyxl.load_workbook(filename=file, read_only=True)
            sheet = workbook.active  # 获取活动的工作表

            # 读取并返回第一个工作表的所有行
            rows = []

            for row in sheet.iter_rows(values_only=True):
                rows.append(list(row))

            datas = [
                {header: value for header, value in zip(rows[0], row)}  # 使用标题行作为字典的键
                for row in rows[1:]  # 从第二行开始迭代
            ]

            result = patient_list_dao.importing(datas)

            if result >= 1:
                info = result
                print(info)
                result = {"code": 20000, "err": 0, "message": "导入成功",
                          "data": {"userinfo": info}}
            else:
                result = {"err": 2, "message": "导入失败", "data": {}}
            return result
        except Exception as e:
            return jsonify({"error": str(e)}), 500

# 验证excel表格解析后可以进行sql新增操作
if __name__ == '__main__':
    data = [['id', 'name', 'identity_card', 'identity_card_sha'],
            [1, '导入1号', '331004200005100872', '11112'],
            [2, '导入2号', '331004200005100875', None],
            [3, '导入3号', '331342200034500872', None]]

    # 跳过标题行，将每一行转换为字典
    result = [
        {header: value for header, value in zip(data[0], row)}  # 使用标题行作为字典的键
        for row in data[1:]  # 从第二行开始迭代
    ]

    # 打印结果以验证
    for item in result:
        print(item)

    demo1 = {'name': '导入1号', 'identity_card': '331004200005100872', 'identity_card_sha': '11112'}
    demo2 = {'name': '导入2号', 'identity_card': '331004200005100875', 'identity_card_sha': None}
    demo3 = {'name': '导入3号', 'identity_card': '331342200034500872', 'identity_card_sha': None}
